package com.hdax.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdax.entity.CancelApply;
import com.hdax.entity.CusIntention;
import com.hdax.entity.CusOrder;
import com.hdax.entity.Customer;
import com.hdax.service.CancelApplyService;
import com.hdax.service.CusOrderService;
import com.hdax.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/cancelApply")
public class CancelApplyController {
    @Autowired
    private CancelApplyService cancelApplyService;
    @Autowired
    private CusOrderService cusOrderService;
    @Autowired
    private CustomerService customerService;

    @GetMapping("/toCancelCreate")
    public ModelAndView toCancelCreate(Integer id){

        ModelAndView modelAndView = new ModelAndView("cancel/cancel_create");
        modelAndView.addObject("cancelApply",cancelApplyService.findCancelApplyByOrderId(id));
        return modelAndView;
    }

    @PostMapping("/saveCancelApply")
    public String savaCancelApply(CancelApply cancelApply){
        System.out.println(cancelApply);
        cancelApply.setHandleState(1);
        cancelApplyService.save(cancelApply);
        Integer orderId = cancelApply.getOrderId();
        CusOrder cusOrder = new CusOrder();
        cusOrder.setId(orderId);
        cusOrder.setOrderState(5);
        cusOrder.setUpdateTime(LocalDateTime.now());
        cusOrderService.updateById(cusOrder);
        return "success";
    }

    @GetMapping("/toCancelApply")
    public ModelAndView toCancelApply(){
        return new ModelAndView("cancel/cancelApply");
    }

    @RequestMapping("/findCancelApply")
    public IPage<CancelApply> findCancelApply(Integer page, Integer limit) {
        IPage<CancelApply> iPage = new Page(page,limit);
        IPage<CancelApply> cancelApply = cancelApplyService.findCancelApply(iPage, new HashMap());
        System.out.println(cancelApply.getRecords());
        System.out.println(cancelApply.getTotal());
        return cancelApply;
    }

    @RequestMapping("/findCancelApplyConditional")
    public IPage<CancelApply> findCancelApplyConditional(@RequestBody Map map) {
        IPage<CancelApply> iPage = new Page<>(Long.parseLong(map.get("page").toString()),Long.parseLong(map.get("limit").toString()));
        IPage<CancelApply> cancelApply = cancelApplyService.findCancelApply(iPage, map);
        return cancelApply;
    }

    @GetMapping("/toCancelProcess")
    public ModelAndView toCancelProcess(Integer id){
        ModelAndView modelAndView = new ModelAndView("cancel/cancel_process");
        modelAndView.addObject("cancelApply",cancelApplyService.findCancelApplyById(id));
        return modelAndView;
    }

    @PostMapping("/processCancelApply")
    public String processCancelApply(CancelApply cancelApply){
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        cancelApply.setHandleState(2);//设置处理状态为已处理
        cancelApplyService.updateById(cancelApply);//更新处理表
        CusOrder cusOrder = new CusOrder();
        cusOrder.setId(cancelApply.getOrderId());
        if(cancelApply.getCancelState()==1){
            cusOrder.setOrderState(4);
            Customer customer = new Customer();
            customer.setId(cancelApply.getCusId());
            customer.setCustomerState(4);
            System.out.println(customer);
            customerService.updateById(customer);
        }else {
            cusOrder.setOrderState(1);
        }
        cusOrderService.updateById(cusOrder);
        return "success";
    }
}
